import {observable,action} from "mobx";
import {WeaTableNew,WeaLogView,WeaForm} from "comsMobx";
import {WeaLocaleProvider} from "ecCom";
import * as Api from "./../apis/privateGroupSetingApi";
import {message,Modal} from "antd";

const {TableStore} = WeaTableNew;
const {LogStore}=WeaLogView;
const {getLabel} =WeaLocaleProvider;
const confirm = Modal.confirm;

class PrivateGroupStore{

  @observable dialogVisible = false;
  @observable tableStore = new TableStore;
  @observable selectedRowKeys = [];
  @observable logVisible = false;
  @observable logStore = new LogStore();
  @observable logType = "";
  @observable logSmallType = "";

  @observable loading = false;

  @observable isCreat = true; 

  /*--编辑弹窗--*/
  @observable form = new WeaForm;
  @observable creatDialogVisible = false ;

  //获取列表数据
  @action.bound getTableData(receiveName = ""){
    const param = {
      ConditionType :"1",
      receiveName  : receiveName,
    }
    Api.getTableData(param).then(data=>{
      if(data.sessionkey){
        this.tableStore.getDatas(data.sessionkey);
      }
    })
  }

  //行选中
  @action.bound rowSelection(selectRow,selectRowInfo){
    this.selectedRowKeys = selectRow;
  }

  //弹窗隐藏
  @action.bound dialogHide(){
    this.dialogVisible = false;
  }

  //弹窗显示
  @action.bound dialogShow(){
    this.dialogVisible = true;
  }

   //编辑/新建 弹窗隐藏
   @action.bound creatDialogHide(){
    this.creatDialogVisible = false;
    this.selectedRowKeys = [];
  }

  //编辑/新建 弹窗显示
  @action.bound creatDialogShow(isCreat = true){
    this.isCreat = isCreat;
    this.getCommReceiveItems();
    this.creatDialogVisible = true;
  }

  //获取form表单
  @action.bound getCommReceiveItems(){
    const params = {
      ConditionType : "1",
      receiveId: this.selectedRowKeys[0]
    }
    Api.getCommReceiveItems(params).then(data=>{
      if(data.api_status){
        this.form = new WeaForm;
        this.form.setCondition(data.groupsCondition)
      }      
    })
  }

  //右侧操作菜单
  @action.bound onOperatesClick(record,index,operate,flag){
    this.selectedRowKeys = [record.id];
    if(flag == "0"){
      this.creatDialogShow(false)
    }else if(flag == "2"){
      this.showConfirm()
    }else if(flag == "3"){
      this.logShow();
    }
  }

  //校验
  @action.bound onCheck(){
    this.form.validateForm().then(data=>{
      if(data.isValid){
        this.saveReceiveGroup()
      }else{
        data.showErrors();
      }
    })
  }
  
  //编辑 新建保存
  @action.bound saveReceiveGroup(){
    this.setLoading(true)
    const formData = this.form.getFormParams();
    const id = this.selectedRowKeys[0];
    const param = {
      ...formData,
      receiveunitType : "1",
      id:id,
    }
    Api.saveReceiveGroup(param).then(data=>{
      this.setLoading(false)
      if(data.api_status){
        this.getTableData();
        this.creatDialogHide();
        message.success(getLabel(22619,"保存成功！"))
      }else{
        message.error(data.api_errormsg)
      }
    })
  }
  
  @action.bound showConfirm() {
  	const _this = this;
	  confirm({
	    title: '您是否确认要删除这项内容',
	    onOk() {
	      _this.deletePrivate()
	    },
	  });
	}


  //删除
  @action.bound deletePrivate(){
    this.setLoading(true)
    const params = {
      ids:this.selectedRowKeys.join(","),
    }   
    Api.deleteReceiveGroup(params).then(data=>{
      this.setLoading(false)
      if(data.api_status ){
        message.success(getLabel(20461,"删除成功"));
        this.getTableData()
        this.selectedRowKeys=[];
      }else{
        message.error(data.api_errormsg);
      }
    })
  }

  //获取日志参数
  @action.bound getPrivateReceiveLogType(){
    Api.getPrivateReceiveLogType().then(data=>{
      this.logType = data.logType;
      this.logSmallType = data.logSmallType;
    })
  }

  //日志显示
  @action.bound logShow(){
    this.logVisible = true;
  } 

  //日志隐藏
  @action.bound logHide(){
    this.logVisible = false;
    this.selectedRowKeys =[];
  } 

  @action.bound setLoading(bool){
    this.loading = bool;
  }

}
export default PrivateGroupStore;